import { Logger } from "./Logger";

export class StringUtil {


    /**
     * 字符串提出按
     * @param formatSrt 字符串 aaaa{0}
     * @param args 参数
     * @returns 
     */
    public static format(formatSrt: string, ...args: any[]) {
        for (let i = 0; i < args.length; i++) {
            let re: RegExp = new RegExp("\\{" + i + "\\}", "gm");
            let matchs = formatSrt.match(re);
            if (matchs) {
                for (let j = 0; j < matchs.length; ++j) {
                    let newParam = args[i];
                    formatSrt = formatSrt.replace(re, newParam);
                }
            }
        }
        return formatSrt;
    }

    /**
     * 判断字符串是不是汉字
     * @param char
     */
    public static isChinese(char: string): boolean {
        if (!char || char.length > 1) {
            return false;
        }
        let reg: RegExp = /[\u4E00-\u9FA5]/;
        if (reg.test(char)) {
            return true;
        }
        return false;
    }

    /**
     * 检查是否是邮箱
     * @param email 效验得字符串
     * @returns 
     */
    public static checkEmail(str: string): boolean {
        let myReg: RegExp = /^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})*$/;
        if (myReg.test(str)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 检测手机号
     * @param str 
     * @returns 
     */
    public static checkPhone(str: string) {
        let myReg: RegExp = /^\d{7,11}$/;
        if (myReg.test(str)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 去掉空格
     * @param str  字符串 
     * @returns 
     */
    public static trim(str: string) {
        let result: string = str.replace(/(^\s+)|(\s+$)/g, "");
        return result.replace(/\s/g, "");
    }

    //获取是否包含大小写
    public static containsLowAndBig(str: string): boolean {
        let reg: RegExp = /(?=.*[a-zA-Z])/;
        return reg.test(str);
    }

    //获取是否包含数字
    public static containsNumber(str: string): boolean {
        let reg: RegExp = /\d/;
        return reg.test(str);
    }

    //至少有6个字符
    public static containsSixStr(str: string): boolean {
        let reg: RegExp = /[\w\S]{6,}/;
        return reg.test(str);
    }

    /** 
     *  以 1,234,567,890.00格式返回金币
     * @param nowValue 
     */
    public static formatMoney(coin: string) {
        let nowValue: number = parseFloat(coin)
        if (isNaN(nowValue)) {
            nowValue = 0;
        }
        // console.log("formatMoney==", nowValue)
        // let result: string = nowValue.toLocaleString("en-US")
        let result: string = nowValue.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
        return result;
    }

    /**
     * json 字符串转换为json
     * @param str 
     * @returns 
     */
    public static JSON_parse(str: string): any {
        try {
            let json = JSON.parse(str);
            return json;
        } catch (error) {
            Logger.error("JSON_parse error=", error)
            return null;
        }
    }

}